using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
using Rolodex.ApplicationControllers;
using System.Security.Principal;
using System.Reflection;

namespace Rolodex
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread()]
        static void Main()
        {
            if (!new WindowsPrincipal(WindowsIdentity.GetCurrent())
                .IsInRole(WindowsBuiltInRole.Administrator))
            {
                // It is not possible to launch a ClickOnce app as administrator directly,
                // so instead we launch the app as administrator in a new process.
                var theProcessInfo = new ProcessStartInfo(
                    Assembly.GetExecutingAssembly().CodeBase);

                // The following properties run the new process as administrator
                theProcessInfo.UseShellExecute = true;
                theProcessInfo.Verb = "runas";

                // Start the new process
                try
                {
                    Process.Start(theProcessInfo);
                }
                catch (Exception)
                {
                    // The user did not allow the application to run as administrator
                    MessageBox.Show("Sorry, this application must be run as Administrator.");
                }

                // Shut down the current process
                Application.Exit();
            }
            else
            {
                Controller theController = new Controller();
                theController.StartApp();
            }
        }
    }
}